﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RC1.student
{
    public partial class main : System.Web.UI.Page
    {
        int user_id;
        int COUNT = 10;

        protected void Page_Load(object sender, EventArgs e)
        {
            user_id = Convert.ToInt32(Session["user_id"]);
            //populateTop10();
            populateNewAssignements();
            populateCorrectedAssignment();
            populateHandedin();
        }

        private void populateNewAssignements()
        {
            tsu2011db4Entities1 db = new tsu2011db4Entities1();
            var attending = (from a in db.attendings
                             join c in db.classes on a.class_id equals c.class_id
                             join ass in db.assignments on a.class_id equals ass.class_id
                             where ass.assignment_deadline > DateTime.Now
                             where a.user_id == user_id
                             select new
                             {
                                 class_id = c.class_id,
                                 class_name = c.class_name,
                                 id = ass.assignment_id,
                                 title = ass.assignment_title,
                                 description = ass.assignment_description,
                                 deadline = ass.assignment_deadline
                             });

            int assignmentCount = 1;
            String newAssignmentTable = "<table border='1' cellpadding='5' width='100%'>";
            newAssignmentTable += "<tr><th>Nr</th><th>Flokkur</th><th>Navn</th><th>Lýsing</th><th>Freist</th><th>Tíð eftir</th><th>Støða</th></tr>";
            foreach (var att in attending)
            {
                if (assignmentCount < COUNT + 1)
                {
                    newAssignmentTable += "<tr>";
                    newAssignmentTable += "<td>" + assignmentCount + "</td>";
                    newAssignmentTable += "<td><a href='viewClass.aspx?id=" + att.class_id + "'>" + att.class_name + "</a></td>";
                    newAssignmentTable += "<td>" + att.title + "</td>";
                    newAssignmentTable += "<td>" + att.description + "</td>";
                    newAssignmentTable += "<td>" + att.deadline + "</td>";
                    TimeSpan remaining = att.deadline - DateTime.Now;
                    newAssignmentTable += "<td>" + remaining.Days + " dagar, " + remaining.Hours + ":" + remaining.Minutes + ":" + remaining.Seconds + "</td>";
                    newAssignmentTable += "<td>Ikki latið inn</td>";
                    newAssignmentTable += "</tr>";
                    assignmentCount++;
                }
            }
            newAssignmentTable += "</table>";
            Label1.Text = newAssignmentTable;
        }

        private void populateCorrectedAssignment()
        {
            tsu2011db4Entities1 db = new tsu2011db4Entities1();
            var correctedAssignments = (from a in db.attendings
                                        join c in db.classes on a.class_id equals c.class_id
                                        join ass in db.assignments on a.class_id equals ass.class_id
                                        join s in db.submissions on ass.assignment_id equals s.assignment_id
                                        join cs in db.corrected_submission on s.submission_id equals cs.submission_id
                                        where a.user_id == user_id
                                        select new
                                        {
                                            class_id = c.class_id,
                                            class_name = c.class_name,
                                            id = ass.assignment_id,
                                            title = ass.assignment_title,
                                            description = ass.assignment_description,
                                            deadline = ass.assignment_deadline,
                                            corDate = cs.corrected_date
                                        });

            int correctedAssignmentCount = 1;
            String newCorrectedTable = "<table border='1' cellpadding='5' width='100%'>";
            newCorrectedTable += "<tr><th>Nr</th><th>Flokkur</th><th>Navn</th><th>Lýsing</th><th>Tíð eftir</th><th>Støða</th><th>Dato</th></tr>";

            foreach (var correctedAssignment in correctedAssignments)
            {
                if (correctedAssignmentCount < COUNT + 1)
                {
                    newCorrectedTable += "<tr>";
                    newCorrectedTable += "<td>" + correctedAssignmentCount + "</td>";
                    newCorrectedTable += "<td><a href='viewClass.aspx?id=" + correctedAssignment.class_id + "'>" + correctedAssignment.class_name + "</td>";
                    newCorrectedTable += "<td>" + correctedAssignment.title + "</td>";
                    newCorrectedTable += "<td>" + correctedAssignment.description + "</td>";
                    newCorrectedTable += "<td>" + correctedAssignment.deadline + "</td>";
                    newCorrectedTable += "<td>Rættað</td>";
                    newCorrectedTable += "<td>" + correctedAssignment.corDate + "</td>";
                    newCorrectedTable += "</tr>";
                    correctedAssignmentCount++;
                }
            }
            
            newCorrectedTable += "</table>";
            Label2.Text = newCorrectedTable;
        }

        private void populateHandedin()
        {
            tsu2011db4Entities1 db = new tsu2011db4Entities1();
            var correctedAssignments = (from a in db.attendings
                             join c in db.classes on a.class_id equals c.class_id
                             join ass in db.assignments on a.class_id equals ass.class_id
                             join s in db.submissions on ass.assignment_id equals s.assignment_id
                             where a.user_id == user_id
                             select new
                             {
                                 class_id = c.class_id,
                                 class_name = c.class_name,
                                 id = ass.assignment_id,
                                 title = ass.assignment_title,
                                 description = ass.assignment_description,
                                 deadline = ass.assignment_deadline
                             });

            int handedInAssignmentCount = 1;
            String newHandedInTable = "<table border='1' cellpadding='5' width='100%'>";
            newHandedInTable += "<tr><th>Nr</th><th>Flokkur</th><th>Navn</th><th>Lýsing</th><th>Freist</th><th>Tíð eftir</th><th>Støða</th></tr>";
            foreach (var correctedAssignment in correctedAssignments)
            {
                if (handedInAssignmentCount < COUNT + 1)
                {
                    newHandedInTable += "<tr>";
                    newHandedInTable += "<td>" + handedInAssignmentCount + "</td>";
                    newHandedInTable += "<td><a href='viewClass.aspx?id=" + correctedAssignment.class_id + "'>" + correctedAssignment.class_name + "</td>";
                    newHandedInTable += "<td>" + correctedAssignment.title + "</td>";
                    newHandedInTable += "<td>" + correctedAssignment.description + "</td>";
                    newHandedInTable += "<td>" + correctedAssignment.deadline + "</td>";
                    TimeSpan remaining = correctedAssignment.deadline - DateTime.Now;
                    if (DateTime.Now >= correctedAssignment.deadline)
                    {
                        newHandedInTable += "<td>Freistin er farin</td>";
                    }
                    else
                    {
                        newHandedInTable += "<td>" + remaining.Days + " dagar, " + remaining.Hours + ":" + remaining.Minutes + ":" + remaining.Seconds + "</td>";
                    }
                    newHandedInTable += "<td>Latið inn</td>";
                    newHandedInTable += "</tr>";

                    handedInAssignmentCount++;
                }
            }

            Label3.Text = newHandedInTable;
        }

        

        private void populateTop10()
        {
            int assignmentCount = 1;
            int correctedAssignmentCount = 1;
            int handedInAssignmentCount = 1;

            int MAX = 11;


            tsu2011db4Entities1 db = new tsu2011db4Entities1();

            var attending = (from a in db.attendings
                             join c in db.classes on a.class_id equals c.class_id
                             where a.user_id == user_id
                             select new
                             {
                                 class_id = c.class_id,
                                 class_name = c.class_name
                             });

            String newAssignmentTable = "<table border='1' cellpadding='5' width='100%'>";
            newAssignmentTable += "<tr><th>Nr</th><th>Flokkur</th><th>Navn</th><th>Lýsing</th><th>Freist</th><th>Tíð eftir</th><th>Støða</th></tr>";

            String newHandedInTable = "<table border='1' cellpadding='5' width='100%'>";
            newHandedInTable += "<tr><th>Nr</th><th>Flokkur</th><th>Navn</th><th>Lýsing</th><th>Freist</th><th>Tíð eftir</th><th>Støða</th></tr>";

            String newCorrectedTable = "<table border='1' cellpadding='5' width='100%'>";
            newCorrectedTable += "<tr><th>Nr</th><th>Flokkur</th><th>Navn</th><th>Lýsing</th><th>Freist</th><th>Tíð eftir</th><th>Støða</th></tr>";
            foreach (var att in attending)
            {
                var assignments = (from ass in db.assignments
                                   orderby ass.assignment_deadline descending
                                   where ass.class_id == att.class_id
                                   select ass);
                foreach (var assignment in assignments)
                {
                    var submission = (from sub in db.submissions
                                      orderby sub.submission_date descending
                                      where sub.user_id == user_id
                                      where sub.assignment_id == assignment.assignment_id
                                      select sub).SingleOrDefault();

                    if (submission == null)
                    {
                        if (assignmentCount < MAX)
                        {
                            newAssignmentTable += "<tr>";
                            newAssignmentTable += "<td>" + assignmentCount + "</td>";
                            newAssignmentTable += "<td><a href='viewClass.aspx?id=" + att.class_id + "'>" + att.class_name + "</a></td>";
                            newAssignmentTable += "<td>" + assignment.assignment_title + "</td>";
                            newAssignmentTable += "<td>" + assignment.assignment_description + "</td>";
                            newAssignmentTable += "<td>" + assignment.assignment_deadline + "</td>";
                            TimeSpan remaining = assignment.assignment_deadline - DateTime.Now;
                            if (DateTime.Now >= assignment.assignment_deadline)
                            {
                                newAssignmentTable += "<td>Freistin er farin</td>";
                            }
                            else
                            {
                                newAssignmentTable += "<td>" + remaining.Days + " dagar, " + remaining.Hours + ":" + remaining.Minutes + ":" + remaining.Seconds + "</td>";
                            }
                            newAssignmentTable += "<td>Ikki rættað</td>";
                            newAssignmentTable += "</tr>";

                            assignmentCount++;
                        }
                    }
                    else
                    {

                        var correctedAssignment = (from ca in db.corrected_submission
                                                   orderby ca.corrected_date descending
                                                   where ca.submission_id == submission.submission_id
                                                   select ca).SingleOrDefault();
                        if (correctedAssignment == null)
                        {
                            if (handedInAssignmentCount < MAX)
                            {
                                newHandedInTable += "<tr>";
                                newHandedInTable += "<td>" + handedInAssignmentCount + "</td>";
                                newHandedInTable += "<td><a href='viewClass.aspx?id=" + att.class_id + "'>" + att.class_name + "</td>";
                                newHandedInTable += "<td>" + assignment.assignment_title + "</td>";
                                newHandedInTable += "<td>" + assignment.assignment_description + "</td>";
                                newHandedInTable += "<td>" + assignment.assignment_deadline + "</td>";
                                TimeSpan remaining = assignment.assignment_deadline - DateTime.Now;
                                if (DateTime.Now >= assignment.assignment_deadline)
                                {
                                    newHandedInTable += "<td>Freistin er farin</td>";
                                }
                                else
                                {
                                    newHandedInTable += "<td>" + remaining.Days + " dagar, " + remaining.Hours + ":" + remaining.Minutes + ":" + remaining.Seconds + "</td>";
                                }
                                newHandedInTable += "<td>Latið inn</td>";
                                newHandedInTable += "</tr>";

                                handedInAssignmentCount++;
                            }
                        }
                        else
                        {
                            if (correctedAssignmentCount < MAX)
                            {
                                newCorrectedTable += "<tr>";
                                newCorrectedTable += "<td>" + correctedAssignmentCount + "</td>";
                                newCorrectedTable += "<td><a href='viewClass.aspx?id=" + att.class_id + "'>" + att.class_name + "</td>";
                                newCorrectedTable += "<td>" + assignment.assignment_title + "</td>";
                                newCorrectedTable += "<td>" + assignment.assignment_description + "</td>";
                                newCorrectedTable += "<td>" + assignment.assignment_deadline + "</td>";
                                TimeSpan remaining = assignment.assignment_deadline - DateTime.Now;
                                if (DateTime.Now >= assignment.assignment_deadline)
                                {
                                    newCorrectedTable += "<td>Freistin er farin</td>";
                                }
                                else
                                {
                                    newCorrectedTable += "<td>" + remaining.Days + " dagar, " + remaining.Hours + ":" + remaining.Minutes + ":" + remaining.Seconds + "</td>";
                                }
                                newCorrectedTable += "<td>Rættað</td>";
                                newCorrectedTable += "</tr>";

                                correctedAssignmentCount++;
                            }
                        }
                    }
                }
            }
            newAssignmentTable += "</table>";
            newHandedInTable += "</table>";
            newCorrectedTable += "</table>";
            

            if (correctedAssignmentCount == MAX && handedInAssignmentCount == MAX && assignmentCount == MAX)
            {
                return;
            }
        }
    }
}